import { useUserStore } from "@/stores";
import { message } from "ant-design-vue";
import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/", redirect: "/home" },
    { path: "/home", component: () => import("@/views/WikiHome.vue") },
    { path: "/about", component: () => import("@/views/WikiAbout.vue") },
    { path: "/doc", component: () => import("@/views/WikiDoc.vue") },
    {
      path: "/admin/ebook",
      component: () => import("@/views/Admin/AdminEbook.vue"),
      meta: { permission: true },
    },
    {
      path: "/admin/category",
      component: () => import("@/views/Admin/AdminCategory.vue"),
      meta: { permission: true },
    },
    {
      path: "/admin/doc",
      component: () => import("@/views/Admin/AdminDoc.vue"),
      meta: { permission: true },
    },
    {
      path: "/admin/user",
      component: () => import("@/views/Admin/AdminUser.vue"),
      meta: { permission: true },
    },
  ],
});

// 访问权限控制
router.beforeEach((to, from, next) => {
  const store = useUserStore();
  if (to.meta.permission) {
    if (store.user?.id) {
      next();
    } else {
      message.info("请先登录");
      next("/");
    }
  } else {
    next();
  }
});

export default router;
